package me.like.diandian.api;

import me.like.diandian.base.BaseConstant;
import me.like.diandian.base.BaseModel;
import me.like.diandian.bean.StartImgBean;
import me.like.diandian.bean.UserLoginBean;
import me.like.diandian.module.ApiModule;
import me.like.diandian.requestbean.LoginRequest;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;


/**
 * Created by Administrator on 2017/1/16.
 */

public class Api {

    public static Api instance;

    public ApiService getService() {
        return service;
    }

    private ApiService service;


    public Api(OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseConstant.API_BASE_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .addConverterFactory(ScalarsConverterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(new NullOnEmptyConverterFactory())
                .addConverterFactory(RsaGsonConverterFactory.create())
                .build();
        service = retrofit.create(ApiService.class);
    }


    public static Api getInstance(OkHttpClient okHttpClient) {
        if (instance == null) {
            instance = new Api(okHttpClient);
        }
        return instance;
    }

    public static Api getInstance() {
        if (instance == null) {
            instance = new Api(ApiModule.getClient());
        }
        return instance;
    }




    /**
     * 发送验证码
     * 发送短信是的发送类型（发送短信的接口需要用到）sendType：
     * USER_REGIST 新用户注册
     * FIND_LOGINPASS 找回登录密码
     *
     * @return
     */
    public Observable<BaseModel<String>> sendPhone(String phone, String sendType) {
        return service.sendPhone(phone, sendType);
    }

    /**
     * POST /api/v1/howeFront/user/messageLoginSubmit
     * 通过验证码登录
     *
     * @param channelId     渠道号1001
     * @param phone
     * @param phoneVaricode
     * @param registType    //（REGIST_IOS REGIST_ANDROID）
     * @return
     */
    public Observable<BaseModel<UserLoginBean>> messageLoginSubmit(String channelId, String phone, String phoneVaricode, String registType) {
        LoginRequest request = new LoginRequest(channelId, phone, phoneVaricode, registType);
        return service.messageLoginSubmit(request);
    }

    public Observable<BaseModel<StartImgBean>> getStartImg() {
        return service.getStartImg();
    }



//
//    /**
//     * 注册
//     *
//     * @param account
//     * @param pwd
//     * @return
//     */
//    public Observable<Register> aRegister(String account, String pwd, String verifCode) {
//        FwsHttpParames parames = new FwsHttpParames();
//        parames.put("account", account);
//        parames.put("verifCode", verifCode);
//        parames.put("pwd", pwd);
//        String channelType = CommonUtils.getAppMetaData(BaseApplication1.getInstance(), "UMENG_CHANNEL");
//        parames.put("channelType", channelType);
//        parames.put("userType", "1");
//        parames.put("platform", "android");
//        parames.put("tokenKey", BqsDF.getTokenKey());
//        String blackBox = FMAgent.onEvent(BaseApplication1.getInstance());
//        parames.put("tongdunKey", blackBox);
//        String packageId = CommonUtils.getAppPkgName();
//        parames.put("packageId", packageId);
//
//        String deviceId = SmAntiFraud.getDeviceId();
//        parames.put("deviceId", deviceId);
//
//        String time = TimeUtils.getSingTime(new Date());
//        String sign = CommonUtils.generateSign(parames, time);
//
//
//        return service.register(sign, time, BaseConstant.accessKey, account, pwd, verifCode, channelType, "1",
//                "android", BqsDF.getTokenKey(), blackBox, packageId, deviceId);
//    }


}
